﻿using System.Collections;
using System.Collections.Generic;
using System.Net;
using Aliyun.OSS;
using Codice.Client.Common;
using UnityEditor;
using UnityEngine;
using static Aliyun.OSS.Model.ListMultipartUploadsResult;

namespace cframework
{
    public static class CommandAlias
    {
        public static Dictionary<string, string[]> aliases = new () {
            { "python3", new string[] { "python" } },
            { "pip3", new string[] { "pip" } }
        };

        public static Dictionary<string, System.Func<string, bool>> cmdCheckFuns = new() {
            {
                "python3", (toolPath) =>
                {
                    var projectPath = System.IO.Path.GetDirectoryName(Application.dataPath);
                    var ret = CommandLineTool.RunCommand2(toolPath, $"--version", projectPath);
                    if(ret.ExitCode == 0)
                    {
                        return ret.StdOut.ToLower().Contains("python");
                    }

                    return false;
                }
            },
        };

        public static bool Check(string toolName, string toolPath)
        {
            if(!CommandAlias.cmdCheckFuns.ContainsKey(toolName))
            {
                return true;
            }

            return cmdCheckFuns[toolName](toolPath);
        }
    }

    public static class CheckToolHelper
    {
        public static bool CheckPythonTool(string toolName)
        {
            var pipPath = DeviceToolPathSetting.GetSystemToolPath("pip3");
            if (string.IsNullOrEmpty(pipPath))
            {
                throw new System.Exception("pip3 not found");
            }

            var projectPath = System.IO.Path.GetDirectoryName(Application.dataPath);
            var ret = CommandLineTool.RunCommand2(pipPath, $"show {toolName}", projectPath);


            //var ret = CommandLineTool.RunCommand2(pipPath, $"list ", projectPath);

            //Debug.LogError($"test {ret.ExitCode} {toolName} {pipPath}  {ret.StdOut} {ret.StdErr}");
            //return false;

            return ret.ExitCode == 0;
        }
    }
}